﻿Imports System
Imports System.CodeDom
Imports System.Collections
Imports System.Globalization

Namespace System.Workflow.Activities.Rules
    Friend Class TypeSymbol
        Inherits TypeSymbolBase
        ' Methods
        Friend Sub New(ByVal type As Type)
            Me.Type = type
            Me.name = type.Name
            If type.IsGenericType Then
                Dim length As Integer = type.Name.LastIndexOf("`"c)
                If (length > 0) Then
                    Dim s As String = type.Name.Substring((length + 1))
                    Me.GenericArgCount = Integer.Parse(s, CultureInfo.InvariantCulture)
                    Me.name = type.Name.Substring(0, length)
                End If
            End If
        End Sub

        Friend Function CanOverload(ByVal typeSym As TypeSymbol) As Boolean
            Return (typeSym.GenericArgCount <> Me.GenericArgCount)
        End Function

        Friend Overrides Function OverloadType(ByVal newTypeSymBase As TypeSymbolBase) As OverloadedTypeSymbol
            Dim symbol As OverloadedTypeSymbol = [TryCast](newTypeSymBase,OverloadedTypeSymbol)
            If (Not symbol Is Nothing) Then
                Return symbol.OverloadType(Me)
            End If
            Dim typeSym As TypeSymbol = [TryCast](newTypeSymBase,TypeSymbol)
            If ((Not typeSym Is Nothing) AndAlso Me.CanOverload(typeSym)) Then
                Return New OverloadedTypeSymbol(Me.name, Me, typeSym)
            End If
            Return Nothing
        End Function

        Friend Overrides Function ParseRootIdentifier(ByVal parser As Parser, ByVal parserContext As ParserContext, ByVal assignIsEquality As Boolean) As CodeExpression
            Return parser.ParseRootTypeIdentifier(parserContext, Me, assignIsEquality)
        End Function

        Friend Overrides Sub RecordSymbol(ByVal list As ArrayList)
            list.Add(Me.Type)
        End Sub


        ' Properties
        Friend Overrides ReadOnly Property Name As String
            Get
                Return Me.name
            End Get
        End Property


        ' Fields
        Friend ReadOnly GenericArgCount As Integer
        Private name As String
        Friend ReadOnly Type As Type
    End Class
End Namespace

